﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateDeviceDomDW]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	-- prepare
	select cast(convert(varchar(50),Created,112) as int) as Created,count(*) as CreatedCount
	into #DeviceDomDW
	from DeviceDom
	group by cast(convert(varchar(50),Created,112) as int);

	-- update existing into data warehouse
	update DeviceDomDW
	set DeviceDomDW.CreatedCount=#DeviceDomDW.CreatedCount
	from DeviceDomDW
	inner join #DeviceDomDW
	on DeviceDomDW.Created=#DeviceDomDW.Created

	-- insert new into data warehouse
	insert into DeviceDomDW
	select Created,CreatedCount
	from #DeviceDomDW
	where not exists (
		select 1
		from DeviceDomDW
		where #DeviceDomDW.Created=DeviceDomDW.Created
	);
END
